package com.platform.core.repository;

import com.platform.comm.repository.CustomBaseRepository;
import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.core.types.dsl.StringPath;
import com.platform.core.entity.QSysUser;
import com.platform.core.entity.SysUser;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.querydsl.binding.SingleValueBinding;
import org.springframework.data.repository.query.Param;

/**
 * Description: 用户-JPA接口
 *
 * @author libin
 * Created on 2019/4/18
 **/
public interface SysUserRepository extends CustomBaseRepository<SysUser, String>, QuerydslBinderCustomizer<QSysUser> {

    /**
     * 获取用户
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Query("select su.id from SysUser su where su.delFlag = '0' and su.username=:username")
    String findByUsername(@Param("username") String username);

    /**
     * 定义查询
     *
     * @param bindings QuerydslBindings
     * @param resource QSysUser
     */
    @SuppressWarnings("NullableProblems")
    @Override
    default void customize(QuerydslBindings bindings, QSysUser resource) {
        bindings.excluding(resource.password, resource.delFlag, resource.status);
        bindings.bind(String.class)
                .first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);
    }
}
